<?php

namespace App\Http\Controllers\Admin;

use App\Models\Admin;
use App\Models\Role;
use Illuminate\Http\Request;
use Illuminate\Mail\Message;
use Mail;

class AdminController extends BaseController
{
    //用户列表
    public function index(Request $request)
    {
        //日期
        $datemin = $request->get('datemin');
        $datemax = $request->get('datemax');
        //账号
        $username = $request->get('username');
        //分页获取数据
        $data = Admin::with('role')->when($datemin, function ($query) use ($datemin) {
            $datemin = date('Y-m-d 00:00:00', strtotime($datemin));
            $query->where('created_at', '>=', $datemin);
        })->when($datemax, function ($query) use ($datemax) {
            $datemax = date('Y-m-d 23:59:59', strtotime($datemax));
            $query->where('created_at', '<', $datemax);
        })->when($username, function ($query) use ($username) {
            $query->where('username', 'like', "%{$username}%");
        })->orderBy('id', 'desc')->withTrashed()->paginate($this->pagesize);
        //实例化一个用户模型对象
        $admin=new Admin();
        return view('admin.admin.index', compact('data','admin'));
    }

    //添加用户页面展示
    public function create()
    {
        //获取所有角色id
        $roleData=Role::get();
        return view('admin.admin.create',compact('roleData'));
    }

    //添加用户数据处理
    public function store(Request $request)
    {
        //表单验证
        $data = $this->validate($request, [
            'username' => 'required|unique:admins,username',
            'truename' => 'required',
            'password' => 'required|confirmed',
            'phone' => 'required|regex:/^1[3-9]\d{9}$/',
            'email' => 'required|email',
            'gender' => 'required',
            'role_id'=>'required'
        ]);
        //数据入库
        $data['password'] = bcrypt($data['password']);
        $model = Admin::create($data);
        //发送通知邮件
        Mail::send('mail.user', compact('data'), function (Message $message) use ($data) {
            //接收邮件用户
            $message->to($data['email'], $data['username']);
            //邮件主题
            $message->subject('创建用户成功通知');
        });
        return redirect(route('admin.admin.index'))->with('success', '添加用户' . $model->truename . '成功');
    }

    //修改用户页面显示
    public function edit(int $id)
    {
        //根据用户id查询用户信息
        $data = Admin::find($id);
        //获取所有角色id
        $roleData=Role::get();
        return view('admin.admin.edit', compact('data','roleData'));
    }

    //修改用户数据处理
    public function update(Request $request, int $id)
    {
        //表单验证
        $data = $this->validate($request, [
            //用户名唯一性验证 unique
            'username' => 'required|unique:admins,username,' . $id,
            'truename' => 'required',
            'password' => 'nullable|confirmed',
            'phone' => 'required|regex:/^1[3-9]\d{9}$/',
            'email' => 'required|email',
            'role_id'=>'required'
        ]);
        if (isset($data['password'])) {
            $data['password'] = bcrypt($data['password']);
        }
        Admin::where('id', $id)->update($data);
        if ($id == auth()->id()) {
            if(isset($data['password'])){
                auth()->logout();
                return redirect(route('admin.login'))->with('success', '个人资料和密码修改成功，请重新登录');
            }
            return redirect(route('admin.center',$id))->with('success', '个人资料修改成功');
        }
        return redirect(route('admin.admin.index'))->with('success', '修改用户成功');
    }

    //删除单个用户数据处理
    public function del(int $id)
    {
        Admin::destroy($id);
        return ['code' => '200', 'msg' => '删除用户成功'];
    }

    //删除所选用户处理
    public function delAll(Request $request)
    {
        Admin::destroy($request->id);
        return ['code' => 200, 'msg' => '批量删除成功'];
    }

    //恢复单个用户数据处理
    public function restore(int $id)
    {
        Admin::onlyTrashed()->where('id', $id)->restore();
        return redirect(route('admin.admin.index'))->with('success', '恢复用户成功');
    }
    //个人资料展示
    //修改用户页面显示
    public function center(int $id)
    {
        //根据用户id查询用户信息
        $data = Admin::find($id);
        return view('admin.admin.center', compact('data'));
    }
}
